/**
 * 
 */
package com.wbcs.jbsf.modal.abs;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * web系统存储用户登录后权限的数据模型
 * @author hawkfly
 */
public abstract class AbsTableSpaceModal
{
    private Log log = LogFactory.getLog(AbsTableSpaceModal.class);
    private Map<String, AbsTableModal> mtables;
    
    public AbsTableSpaceModal(){
        mtables = new HashMap<String, AbsTableModal>();
    }
    
    public AbsTableSpaceModal(String...tablenames){
        mtables = new HashMap<String, AbsTableModal>();
        for(int i=0;i<tablenames.length;i++)
        {
            mtables.put(tablenames[i],new AbsTableModal(tablenames[i]){});
        }
    }
    
    public AbsTableModal getTable(String tablename){
        if(mtables.containsKey(tablename))
            return mtables.get(tablename);
        else
            return null;
    }
    
    public AbsTableModal newTable(String tablename){
        if(mtables.containsKey(tablename)){
            String errormsg = "指定的tablename重复！";
            log.error(errormsg,new Exception(errormsg));
            return null;
        }
        AbsTableModal newtable = new AbsTableModal(tablename){};
        mtables.put(tablename,newtable);
        return newtable;
    }
    
    public AbsTableSpaceModal addTable(AbsTableModal table){
        
        return this;
    }
    
    public AbsTableSpaceModal addTables(AbsTableModal[] tables){
        
        return this;
    }
    
    public Map<String, Object> findRow(String key, AuthTypes tablename){
        
        return null;
    }
    
    public Iterator<String> iterator(){
        return mtables.keySet().iterator();
    }
    
    /**
     * 与JAVA Collection结构兼容
     * @return
     */
    public Map<String, List<Map<String, Object>>> parse2AllCollection(){
        Map<String, List<Map<String, Object>>> rtnmap = new HashMap<String, List<Map<String, Object>>>();
        Iterator<String> it = mtables.keySet().iterator();
        while(it.hasNext()){
            String key = it.next();
            AbsTableModal tablemodal = mtables.get(key);
            rtnmap.put(key,tablemodal.getAllstDatas2Collection());
        }
        return rtnmap;
    }
    
    enum AuthTypes{
        common, wbcs;
    }
}

